import Vue from 'vue'
import VueRouter from 'vue-router'

import Reg from '@/views/Reg/Reg'
import Login from '@/views/Login/Login'
import Main from '@/views/Main/Main'
import store from '@/store'

import Home from '@/views/Menus/Home/Home'

import UserInfo from '@/views/Menus/User/UserInfo'

import UserAvatar from '@/views/Menus/User/UserAvatar'

import UserPwd from '@/views/Menus/User/UserPwd'

import ArtCate from '@/views/Menus/Article/ArtCate'
// 把下面的代码粘贴到路由模块中对应的位置，即可防止路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    component:Main,
    redirect:'/home',
    children:[
      {
        path:'home',
        component:Home
      },
      {
        path:'user-info',
        component:UserInfo
      },
      {
        path:'user-avatar',
        component:UserAvatar
      },
      {
        path:'user-pwd',
        component:UserPwd
      },
      {
        path:'art-cate',
        component:ArtCate
      }
    ]
  },
  {
    path:'/reg',
    component:Reg
  },
  {
    path:'/Login',
    component:Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  const token=store.state.token

  if(to.path==='/reg'||to.path==='/login'){
    next()
  }else{
    if(!token){
      next('/login')
    }else{
      next()
    }
  }

  // if(!token&&(to.path==='/reg'||to.path==='/login')){
  //   next('/login')
  // }else{
  //   next()
  // }

})

export default router
